import { config } from '../../config.js';
import { Common } from '../../common/common.js';
import { MyModel } from '../../../../modules/my.js';

const my = new MyModel();
const common = new Common();

Page({
  data: {
    eid: null, //学历id
    edu: config.data.edu, // 学历
    entryTime: null, // 入学时间
    quitTime: null, // 毕业时间
    eduValue: null, // 学校
    eduCation: null, // 学历
    major: null // 专业
  },
  bindChange: function(e) {
    let _this = this;
    switch(e.target.id) {
      // 入学时间
      case 'entrytime':
        _this.setData({
          entryTime: e.detail.value
        });
      break;
      // 毕业时间
      case 'quittime':
        _this.setData({
          quitTime: e.detail.value
        });
      break;
      // 学历
      case 'education':
        _this.setData({
          eduCation: e.detail.value
        });
      break;
      default:
      break;
    }
  },
  submit(e) {
    let value = e.detail.value;
    if(value) {
      let data = {
        eid: this.data.eid,
        resumeId: common.getRid(),
        entranceTime: this.data.entryTime,
        graduationTime: this.data.quitTime,
        school: value.eduValue,
        degree: this.data.eduCation,
        specialty: value.major
      };
      if(data['entranceTime'] == '') {
        wx.showToast({
          title: '入学时间不能为空',
          icon: 'none'
        });
        return false
      }else if(data['graduationTime'] == '') {
        wx.showToast({
          title: '毕业时间不能为空',
          icon: 'none'
        });
        return false
      }else if(data['school'] == null || data['school'] == '') {
        wx.showToast({
          title: '学校不能为空',
          icon: 'none'
        });
        return false
      }else if(data['degree'] == null ||data['degree'] == '') {
        wx.showToast({
          title: '学历不能为空',
          icon: 'none'
        });
        return false
      }else if(data['specialty'] == null || data['specialty'] == '') {
        wx.showToast({
          title: '专业不能为空',
          icon: 'none'
        });
        return false
      }
      if(data['entranceTime'] > data['graduationTime']) {
        wx.showToast({
          title: '入学时间不能大于毕业时间!',
          icon: 'none'
        });
        return false
      }
      console.log(data);
      // 提交数据
      my.editEducation(data)
      .then(res => {
        if(res.code == 2) {
          wx.showToast({
            title: '修改成功',
            icon: 'none',
            duration: 2000
          });
          setTimeout(() => {
            wx.redirectTo({
              url: '../index/index'
            });
          }, 2000)
        }else {
          wx.showToast({
            title: '修改失败，请稍后重试！',
            icon: 'none',
            duration: 2000
          });
        }
      })
    } 
  },
  onLoad: function (options) {
    let eid = options.eid;
    if(eid) {
      this.setData({
        eid: eid
      })
    }
    // 数据回显
    my.showEducation(eid)
    .then(res => {
      if(res.code == 2) {
        res = res.data.education;
        this.setData({
          entryTime: res.entranceTime,
          quitTime: res.graduationTime,
          eduValue: res.school,
          eduCation: res.degree,
          major: res.specialty,
          loading: true
        })
      }
    })
  }
})